<template>
  <div class="settingUserBox">
    <el-form
      :model="ruleForm"
      status-icon
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户" prop="user">
        <el-input
          class="bin_input"
          v-model.number="username"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          class="bin_input"
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          class="bin_input"
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <p style="color: red; font-size: 16px; line-height: 20px">
        提示：密码请输入数字、大小写字母和特殊字符中的两者以上的组合，且长度不小于10位
      </p>

      <el-form-item>
        <el-button class="bin_button" type="primary" @click="submitForm()"
          >保存</el-button
        >
        <el-button class="bin_button" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "",
  props: {},
  data() {
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
      },
    };
  },
  computed: {
    username() {
      return this.$store.state.username;
    },
  },
  created() {},
  mounted() {},
  methods: {
    validatePassword(password) {
      const lengthCheck = password.length >= 10;
      const hasLowerCase = /[a-z]/.test(password);
      const hasUpperCase = /[A-Z]/.test(password);
      const hasNumber = /\d/.test(password);
      const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);

      const countTypes = [
        hasLowerCase ? 1 : 0,
        hasUpperCase ? 1 : 0,
        hasNumber ? 1 : 0,
        hasSpecialChar ? 1 : 0,
      ].reduce((sum, val) => sum + val, 0);

      return lengthCheck && countTypes >= 2;
    },
    submitForm() {
      if (this.ruleForm.pass != this.ruleForm.checkPass) {
        this.$message({
          message: "两次输入密码不一致，请检查",
          type: "warning",
        });
        return false;
      }
      if (!this.validatePassword(this.ruleForm.checkPass)) {
        this.$message({
          message: "密码输入错误，请详细阅读提示规则",
          type: "warning",
        });
        return false;
      }
      if (!this.validatePassword(this.ruleForm.pass)) {
        this.$message({
          message: "密码输入错误，请详细阅读提示规则",
          type: "warning",
        });
        return false;
      }
      let obj = {
        username: this.username,
        password: this.ruleForm.pass,
      };
      this.$api.updatePassword(obj).then((res) => {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.$router.push({ path: "/login" });
        this.$store.dispatch("setUserDialogShow", false);
      });
    },
    resetForm() {
      this.ruleForm = {
        pass: "",
        checkPass: "",
      };
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-picker-panel {
  background: #072f46;
}

::v-deep .el-form-item__label {
  font-family: Source Han Sans CN, Source Han Sans CN;
  font-weight: 500;
  font-size: 18px;
  color: #fff;
}

.settingUserBox {
}
</style>
